<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/12/0012
 * Time: 9:59
 */

namespace app\sports_admin\service;


use app\exception\WatchInstructionsRecordInfoException;
use think\Cache;
use think\Config;
use think\Request;
use app\sports_admin\model\WatchInstructionsRecord as WatchInstructionsRecordModel;
use think\Session;

class WatchInstructionsRecordInfo
{
    /**
     * 添加指令下发记录
     */
    public static function open()
    {
        $data = self::getParams();
        $number = [
            ['number'=>$data['number'],'high'=>160,'low'=>40],
        ];
        $info['info']['number'] = $number;
        $info['info']['instructions']= 'monitoring';
        $info['info']['status']= 'open';
        $info['info']['uid']= 111111;
        //要加密的签名
        $info['info']['skin'] = getRandChar(15);
        //获取秘钥
        $key = Config::get('skin');
        //签名
        $info['info']['token'] = authcode($info['info']['skin'],'ENCODE',$key['key'],0);
        // 推送的数据，设备编号字段
        $data = $info['info'];
        $client = stream_socket_client('tcp://39.105.192.86:5679', $errno, $errmsg, 1);
        //发送数据，注意5679端口是Text协议的端口，Text协议需要在数据末尾加上换行符
        fwrite($client, json_encode($data)."\n");
        //读取推送结果
        $returnInfo = json_decode(fread($client, 8192),true);
        if(isset($returnInfo['errorCode'])){
            throw new WatchInstructionsRecordInfoException([
                'errorCode'=>400,
                'msg'=>'设备下发指令失败'
            ]);
        }
        return $returnInfo;
    }
    /**
     * 关闭检测模式
     */
    public static function close()
    {
        $data = self::getParams();
        $number = [$data['number']];
        $info['info']['number'] = $number;
        $info['info']['instructions']= 'monitoring';
        $info['info']['status']= 'close';
        $info['info']['uid']= 111111;
        //要加密的签名
        $info['info']['skin'] = getRandChar(15);
        //获取秘钥
        $key = Config::get('skin');
        //签名
        $info['info']['token'] = authcode($info['info']['skin'],'ENCODE',$key['key'],0);
        // 推送的数据，设备编号字段
        $data = $info['info'];
        $client = stream_socket_client('tcp://39.105.192.86:5679', $errno, $errmsg, 1);
        //发送数据，注意5679端口是Text协议的端口，Text协议需要在数据末尾加上换行符
        fwrite($client, json_encode($data)."\n");
        $returnInfo = json_decode(fread($client, 8192),true);
        if(isset($returnInfo['errorCode'])){
            throw new WatchInstructionsRecordInfoException([
                'errorCode'=>400,
                'msg'=>'设备下发指令失败'
            ]);
        }
        return $returnInfo;
    }
    /**
     * 下发编号
     */
    public static function number()
    {
        $data = self::getParams();
        $number = [
            ['number'=>$data['number'],'serialNumber'=>"3-76-2-99-1"]
        ];
        $info['info']['number'] = $number;
        $info['info']['instructions']= 'number';
        $info['info']['uid']= 111111;
        //要加密的签名
        $info['info']['skin'] = getRandChar(15);
        //获取秘钥
        $key = Config::get('skin');
        //签名
        $info['info']['token'] = authcode($info['info']['skin'],'ENCODE',$key['key'],0);
        // 推送的数据，设备编号字段
        $data = $info['info'];
        $client = stream_socket_client('tcp://39.105.192.86:5679', $errno, $errmsg, 1);
        //发送数据，注意5679端口是Text协议的端口，Text协议需要在数据末尾加上换行符
        fwrite($client, json_encode($data)."\n");
        $returnInfo = json_decode(fread($client, 8192),true);
        if(isset($returnInfo['errorCode'])){
            throw new WatchInstructionsRecordInfoException([
                'errorCode'=>400,
                'msg'=>'设备下发指令失败'
            ]);
        }
        return $returnInfo;
    }
    /**
     * 重启指令
     */
    public static function restartOrShutdown()
    {
        $params = self::getParams();
        $info['info']['number'] = $params['number'];
        $info['info']['instructions']= $params['type'];
        $info['info']['uid']= 111111;
        //要加密的签名
        $info['info']['skin'] = getRandChar(15);
        //获取秘钥
        $key = Config::get('skin');
        //签名
        $info['info']['token'] = authcode($info['info']['skin'],'ENCODE',$key['key'],0);
        // 推送的数据，设备编号字段
        $data = $info['info'];
        $client = stream_socket_client('tcp://39.105.192.86:5679', $errno, $errmsg, 1);
        //发送数据，注意5679端口是Text协议的端口，Text协议需要在数据末尾加上换行符
        fwrite($client, json_encode($data)."\n");
        $returnInfo = json_decode(fread($client, 8192),true);
        return $returnInfo;
    }
    /**
     * 开启或关闭关爱模式
     */
    public static function careOrCloseCare()
    {
        $params = self::getParams();
        $info['info']['number'] = $params['number'];
        $info['info']['instructions']= $params['type'];
        $info['info']['uid']= 111111;
        //要加密的签名
        $info['info']['skin'] = getRandChar(15);
        //获取秘钥
        $key = Config::get('skin');
        //签名
        $info['info']['token'] = authcode($info['info']['skin'],'ENCODE',$key['key'],0);
        // 推送的数据，设备编号字段
        $data = $info['info'];
        $client = stream_socket_client('tcp://39.105.192.86:5679', $errno, $errmsg, 1);
        //发送数据，注意5679端口是Text协议的端口，Text协议需要在数据末尾加上换行符
        fwrite($client, json_encode($data)."\n");
        $returnInfo = json_decode(fread($client, 8192),true);
        return $returnInfo;
    }
    public static function mailList()
    {
        $params = self::getParams();
        $info['info']['number'] = $params['number'];
        $info['info']['instructions']= 'mailList';
        $info['info']['uid']= 111111;
        $info['info']['mailList'] = "11111111111+65200+张三";
        //要加密的签名
        $info['info']['skin'] = getRandChar(15);
        //获取秘钥
        $key = Config::get('skin');
        //签名
        $info['info']['token'] = authcode($info['info']['skin'],'ENCODE',$key['key'],0);
        // 推送的数据，设备编号字段
        $data = $info['info'];
       // return json($data);
        $client = stream_socket_client('tcp://39.105.192.86:5679', $errno, $errmsg, 1);
        //发送数据，注意5679端口是Text协议的端口，Text协议需要在数据末尾加上换行符
        fwrite($client, json_encode($data)."\n");
        $returnInfo = json_decode(fread($client, 8192),true);
        return $returnInfo;
    }
    /**
     * 获取参数
     */
    private static function getParams()
    {
        return Request::instance()->param();
    }
}